import dao from '@/api/dao';
import { ref, reactive } from 'vue';

/**
 * 用户数据服务
 * 负责管理用户信息查询、位置数据管理等功能
 */
class UserService {
  constructor() {
    this.userList = ref([]);
    this.selectedUser = ref(null);
    this.userPositionData = ref(null);
    this.searchParams = reactive({
      name: ''
    });
    this.queryParams = reactive({
      id: '',
      name: '',
      startTime: '',
      endTime: '',
      pageNo: 1,
      pageSize: 100,
      createPath: false,
      trafficMode: 'car'
    });
    this.positionData = reactive({
      id: '',
      uid: '',
      lon: '',
      lat: '',
      createTime: ''
    });
  }
  
  /**
   * 重置查询参数
   */
  resetQueryParams() {
    this.queryParams.id = '';
    this.queryParams.name = '';
    this.queryParams.startTime = '';
    this.queryParams.endTime = '';
    this.queryParams.pageNo = 1;
    this.queryParams.pageSize = 100;
    this.queryParams.createPath = false;
    this.queryParams.trafficMode = 'car';
  }
  
  /**
   * 重置位置数据
   */
  resetPositionData() {
    this.positionData.id = '';
    this.positionData.uid = '';
    this.positionData.lon = '';
    this.positionData.lat = '';
    this.positionData.createTime = '';
  }
  
  /**
   * 查询用户信息
   * @param {Object} params - 查询参数
   * @returns {Promise} 查询结果Promise
   */
  async searchUsers(params = this.searchParams) {
    try {
      const response = await dao.getUser(params);
      
      if (response.data.code !== 200) {
        throw new Error(response.data.msg || '查询用户失败');
      }
      
      this.userList.value = response.data.data || [];
      return {
        success: true,
        data: this.userList.value,
        message: response.data.msg
      };
    } catch (error) {
      console.error('查询用户失败:', error);
      return {
        success: false,
        error: error.message,
        message: '查询用户失败'
      };
    }
  }
  
  /**
   * 选择用户
   * @param {String} userId - 用户ID
   * @returns {Object} 选中的用户信息
   */
  selectUser(userId) {
    if (!this.userList.value || this.userList.value.length === 0) {
      return null;
    }
    
    const user = this.userList.value.find(u => u.id === userId);
    if (user) {
      this.selectedUser.value = userId;
      this.queryParams.id = userId;
      this.queryParams.name = user.name;
    } else {
      this.selectedUser.value = null;
    }
    
    return user;
  }
  
  /**
   * 获取用户轨迹数据
   * @param {Object} params - 查询参数，不传则使用当前queryParams
   * @returns {Promise} 查询结果Promise
   */
  async getUserPositions(params = this.queryParams) {
    try {
      const response = await dao.getUserPosition(params);
      
      if (response.data.code !== 200) {
        throw new Error(response.data.msg || '获取位置数据失败');
      }
      
      this.userPositionData.value = response.data.data || { positions: [] };
      
      return {
        success: true,
        data: this.userPositionData.value,
        message: response.data.msg
      };
    } catch (error) {
      console.error('获取位置数据失败:', error);
      return {
        success: false,
        error: error.message,
        message: '获取位置数据失败'
      };
    }
  }
  
  /**
   * 添加位置数据
   * @param {Object} position - 位置数据，不传则使用当前positionData
   * @returns {Promise} 添加结果Promise
   */
  async addPosition(position = this.positionData) {
    try {
      const response = await dao.addPositionData(position);
      
      if (response.data.code !== 200) {
        throw new Error(response.data.msg || '添加位置数据失败');
      }
      
      return {
        success: true,
        data: response.data.data,
        message: response.data.msg
      };
    } catch (error) {
      console.error('添加位置数据失败:', error);
      return {
        success: false,
        error: error.message,
        message: '添加位置数据失败'
      };
    }
  }
  
  /**
   * 获取交通方式选项
   * @returns {Array} 交通方式选项数组
   */
  getTrafficModeOptions() {
    return [
      { value: 'car', label: '汽车' },
      { value: 'bike', label: '自行车' },
      { value: 'foot', label: '步行' }
    ];
  }
}

// 创建单例实例
const userService = new UserService();

export default userService; 